//
//  smSearchListController.m
//  shimaoHouse
//
//  Created by amy zhang on 12-6-27.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smSearchListController.h"
#import "smListDetailControllerViewController.h"
#import "HTTPImageView.h"
#import "smMboSmTbProject.h"
#import "smMboSmMboDB.h"
#import "smloginController.h"

@interface smSearchListController ()<UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate>

@property(strong,nonatomic) UITableView* myTableView;
@property (nonatomic, retain) NSArray *projectList;
@property(strong,nonatomic)UISearchBar* searchBar;

@end

@implementation smSearchListController

@synthesize myTableView=_myTableView;
@synthesize projectList=_projectList;
@synthesize searchBar=_searchBar;


- (void)viewDidLoad
{
    [super viewDidLoad];  
    [self.navigationController.navigationBar setHidden:YES];
        
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"project_background.png"]]];
    
    UIImageView* innerImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"project_title_manage.png"]];
    innerImageView.frame=CGRectMake(0, 0, 768, 70);
         
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, self.view.bounds.size.width, 
                                                                     self.view.bounds.size.height-150) 
                                                    style:UITableViewStylePlain];  
    [self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.myTableView setDelegate:self];
    [self.myTableView setDataSource:self];
    
    self.searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 80)];  
    self.searchBar.placeholder=@"输入项目名称查询";  
    self.searchBar.delegate =self;  
    self.myTableView.tableHeaderView = self.searchBar;  
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;  
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;  
    UIImage* searchbarImg = [UIImage imageNamed:@"search.png"];
    [self.searchBar setBackgroundImage:searchbarImg];
    [self.view addSubview: self.myTableView];
    [self.view addSubview:innerImageView]; 
}



- (void)viewWillAppear:(BOOL)animated {
     //清空searchBar文字
    self.searchBar.text=nil;
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    [smMboSmMboDB beginSynchronize];
    SUPObjectList* list=[smMboSmTbProject findAll];
   
    NSString* temp = [NSString stringWithFormat:@"list size is %d",[list size]];
    
    NSLog(temp,nil);
    
    if ([list length] > 0)
    {
        for (smMboSmTbProject * oneRec in list)
        {
            [array addObject:oneRec];
        }
    }
    self.projectList = array;
   
   [self.myTableView reloadData];
   [super viewWillAppear:animated]; 
}


- (void)viewDidAppear:(BOOL)animated {
  
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.projectList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]] |
            [v isKindOfClass:[HTTPImageView class]]|
            [v isKindOfClass:[UISearchBar class]]) {
            [v removeFromSuperview];
        }
    }
    
    NSUInteger row = [indexPath row];
    smMboSmTbProject *project = [self.projectList objectAtIndex:row];
    
    //加载图片
    UIImage* tempImg = [UIImage imageNamed:@"photoloading.png"];
    HTTPImageView* imgView = [[HTTPImageView alloc] init];     
    [imgView setUrl:[NSString stringWithFormat:@"http://10.91.2.14:8080/upload_file/mobile/%@",[project imagePath]]];
    [imgView setPlaceHolder:tempImg];
    [imgView setFrame:CGRectMake(15, 5, 200, 155)];
    imgView.layer.cornerRadius = 8;  
    imgView.layer.masksToBounds = YES; 
    [cell addSubview:imgView];
    
    //项目名称
    UILabel*tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(230, 10, 600, 50)];
    [tempLabel setText:[[NSString alloc] initWithFormat:@"项目名称：%@",project.projectName]];
    [tempLabel setTextColor:[UIColor colorWithRed:9/255.0 green:119/255.0 blue:179/255.0 alpha:1.0]];
    tempLabel.font = [UIFont systemFontOfSize:29];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:tempLabel];
    //项目简述
    UILabel*tempLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(230, 45, 480, 120)];
    tempLabel2.numberOfLines = 4;  
    tempLabel2.lineBreakMode = UILineBreakModeWordWrap|UILineBreakModeTailTruncation;  
    [tempLabel2 setText:[[NSString alloc] initWithFormat:@"项目简介：\n       %@",project.projectDescription]];    
    tempLabel2.font=[UIFont systemFontOfSize:21];
    [tempLabel2 setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];
    [tempLabel2 setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:tempLabel2];
    
    UIImage* seletedImg = [UIImage imageNamed:@"tableview_seletedBakground.png"];    
    UIImageView *seletedImgView =[[UIImageView alloc] initWithImage:seletedImg];
    [cell setSelectedBackgroundView:seletedImgView];
    
    
    UIImage* backImg = [UIImage imageNamed:@"tableview_background.png"];    
    UIImageView *backImgView =[[UIImageView alloc] initWithImage:backImg];
    [cell setBackgroundView:backImgView];
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 171;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   
    
    
    NSUInteger row = [indexPath row];
    smMboSmTbProject *project = [self.projectList objectAtIndex:row];
    
    smListDetailControllerViewController* listDetailControall = [[smListDetailControllerViewController alloc] init];
    [listDetailControall setSelectRowAtIndexObject:project];
    [self.navigationController pushViewController:listDetailControall animated:YES];
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
        
    [searchBar resignFirstResponder];//取消键盘

    NSMutableArray *array = [[NSMutableArray alloc] init];
    SUPQuery *query = [SUPQuery getInstance];
    query.testCriteria = [SUPAttributeTest match:@"projectName" :searchBar.text];
    SUPObjectList* list=[smMboSmTbProject findWithQuery:query];
    if ([list length] > 0)
    {
        for (smMboSmTbProject * oneRec in list)
        {
            [array addObject:oneRec];
        }
    }
    self.projectList = array;    
    [self.myTableView reloadData];
    
}

@end
